//
//  PhotoQualityViewController.m
//  图片故事
//
//  Created by 熊坤 on 16/3/1.
//  Copyright © 2016年 helloApp. All rights reserved.
//

#import "PhotoQualityViewController.h"
#import "Header.h"
#import "PhotoQualitTableViewCell.h"
#import "LocaData.h"

static NSInteger seletedRow = 1;

@interface PhotoQualityViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *photoTableView;

@end

@implementation PhotoQualityViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:Bg_Color];
    [self initNavigation];
    [self.view addSubview:self.photoTableView];

}

- (void)initNavigation {
    
    self.title = @"图片质量";
    
    UIBarButtonItem *backItme = [[UIBarButtonItem alloc] initWithTitle:@"返回 " style:UIBarButtonItemStylePlain target:self action:@selector(action_backItmeClickEvent)];
    
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    
    self.navigationItem.leftBarButtonItem = backItme;
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : [UIFont systemFontOfSize:20]}];
}

#pragma mark -- 点击事件
- (void)action_backItmeClickEvent {
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma mark -- tableView 代理方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(seletedRow == [indexPath row])
        return;
    seletedRow = [indexPath row];
    [tableView  reloadData];
    
    // 将图片质量保存
    [LocaData setPhotoQualitWithIndex:indexPath.row];
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma mark -- tableView 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    PhotoQualitTableViewCell *photoCell = [tableView dequeueReusableCellWithIdentifier:@"Photo_Cell"];
    
    if (!photoCell) {
        
        photoCell = [[PhotoQualitTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Photo_Cell"];
    }
    
    if ([LocaData readPhotoQualit] == indexPath.row) {
        [photoCell setAccessoryType:UITableViewCellAccessoryCheckmark];

    } else {
        [photoCell setAccessoryType:UITableViewCellAccessoryNone];

    }
    
    [photoCell setBackgroundColor:Btn_Color];
    
    [photoCell setPhotoModel:[[LocaData readSetPhotoQualit] objectAtIndex:indexPath.row]];
    
    return photoCell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return My_Height(60);
}

#pragma mark -- 懒加载
- (UITableView *)photoTableView {
    
    if (!_photoTableView) {
        
        _photoTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -My_Height(20), SCREEN_W, SCREEN_H) style:UITableViewStyleGrouped];
        
        [_photoTableView setDataSource:self];
        
        [_photoTableView setSeparatorColor:[UIColor grayColor]];
        
        [_photoTableView setBackgroundColor:Bg_Color];
        
        [_photoTableView setDelegate:self];
        
        UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        
        [_photoTableView setTableHeaderView:headView];
    }
    return _photoTableView;
}

@end
